package com.tarim.wheat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tarim.wheat.model.Message;
import com.tarim.wheat.service.MessageService;
import com.tarim.wheat.util.Constants;
import com.tarim.wheat.util.JsonResult;
import com.tarim.wheat.vo.Criteria;
import com.tarim.wheat.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    /**
     * 信息录入
     * @param message
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult insert(Message message){
        JsonResult js;
        try {
            int i = messageService.insert(message);
            if(i == 0){
                js = new JsonResult(Constants.STATUS_FAIL, "录入失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "录入成功");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "录入异常");
        }
        return js;
    }

    /**
     * 信息删除
     * @param param
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@RequestBody String param){
        JsonResult js;
        try {
            Map map = JSONObject.parseObject(param, Map.class);
            List<Message> messages = JSONArray.parseArray(map.get("messages").toString(), Message.class);
            int i = messageService.updateForDelete(messages);
            if(i == 0){
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

    /**
     * 获取用户录入记录（分页）
     * @param pageIndex
     * @param pageSize
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getByUser")
    @ResponseBody
    public JsonResult selectByUser(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "userId", defaultValue = "") Long userId){
        JsonResult js;
        try {
            if (userId == null){
                return new JsonResult(Constants.STATUS_FAIL, "请先登录账号");
            }
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", userId);
            pageBean = messageService.selectByUser(criteria, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "成功", pageBean);
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return js;
    }
}
